package cn.psvmc.cxzapi.controller.customer;

import cn.psvmc.cxzapi.enums.ResultCode;
import cn.psvmc.cxzapi.model.common.DetailEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.customer.TCustomer;
import cn.psvmc.cxzapi.model.customer.TCustomerAuth;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.customer.TCustomerService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 客户管理
 *
 * @Author auto
 * @Date 2024-08-12
 */
@RestController
@RequestMapping("/customer")
public class TCustomerController {

    @Resource
    private TCustomerService tCustomerService;

    /**
     * @Author auto 2024-08-12 新增客户表
     * @Param tCustom 客户表
     * @Return cn.psvmc.cxzapi.response.Result
     */
    @PostMapping("/add")
    public Result add(@RequestBody TCustomer tCustomer) {
        ToolUtils.validation(new Object[]{tCustomer.getPhone(), "手机号", 2});
        if (ToolUtils.checkPhone(tCustomer.getPhone())) {
            return ResultGenerator.genFailResult(ResultCode.PHONE_ERROR);
        }
        return tCustomerService.add(tCustomer);
    }

    /**
     * @Author auto 2024-08-12 更新客户表
     * @Param tCustom 客户表模型
     * @Return cn.psvmc.cxzapi.response.Result
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody TCustomer tCustomer) {
        ToolUtils.validation(new Object[]{tCustomer.getCustomerid(), "id", 1, tCustomer.getRversion(), "版本", 1});
        int updateResult = tCustomerService.edit(tCustomer);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
     * @Description 查询客户表详情
     * @Date 2024/8/12 20:19
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tCustomerService.detail(id));
    }

    /**
     * @Author auto 2024-08-12 查询客户表列表
     * @Param entity 分页查询通用实体类
     * @Return cn.psvmc.cxzapi.response.Result
     */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<TCustomer> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tCustomerService.list(queryPage, entity));
    }

    /**
     * @Description 选择列表
     * @Date 2024/8/19 8:57
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public PageResult2 listForSel(@RequestBody SearchEntity entity) {
        Page<TCustomer> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tCustomerService.listForSel(queryPage, entity));
    }

    /**
     * @Description 获取临时访问凭证
     * @Date 2024/8/12 20:49
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_sts")
    public Result getSts() {
        return tCustomerService.getSts();
    }

    /**
     * @Description 使用临时访问凭证上传文件至OSS
     * @Date 2024/8/12 21:32
     * @Author YWX
     * @Param [file, map]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/upload_oss")
    public Result uploadOss(@RequestParam("file") MultipartFile file, @RequestParam Map<String, String> map) {
        return tCustomerService.uploadOss(file, map);
    }

    /**
     * @Description 获取认证资料
     * @Date 2024/8/24 10:39
     * @Author YWX
     * @Param [auth]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_auth")
    public Result getAuth(@RequestBody TCustomerAuth auth) {
        Integer customerid = auth.getCustomerid();
        ToolUtils.validation(new Object[]{
                customerid, "客户id", 1
        });
        return ResultGenerator.genSuccessResult("获取成功！", tCustomerService.getAuth(customerid));
    }

}
